import { ajax } from "@/utils/ajax";

// 老师类型定义
export interface Teacher {
    id: number;
    name: string;
    age?: number;
    sex: number;
    birthday: string;
    idCard: string;
    phone?: string;
    createTime: string;
    position: string;
    groupId?: string; // 小组ID，多个用英文逗号分隔
    groupName?: string[];
    password?: string;
}

// 小组类型定义
export interface Group {
    id: number;
    name: string;
    description?: string;
}

// 新增老师
export interface TeacherForAdd {
    name: string;
    idCard: string;
    phone?: string;
    position: string;
}

// 编辑老师
export interface TeacherForEdit {
    id: number;
    name: string;
    idCard: string;
    phone?: string;
    createTime: string;
    position: string;
    groupId?: string;
}

// 评价
export interface Evaluation {
    id?: number;
    studentId: number;
    teacherId: number;
    evaluation: string;
    createTime?: string;
    teacherName?: string;
    studentName?: string;
}

// 获取老师所在小组的学生
export const getTeacherGroupStudents = (groupId: string) => {
    return ajax.get("/teacher/getStudent", {
        params: { groupId }
    });
};

// 获取老师的评价记录
export const getTeacherEvaluations = (teacherId: number) => {
    return ajax.get("/teacher/getEvaluation", {
        params: { teacherId }
    });
};

// 添加对老师评价
export const addTeacherEvaluation = (data: Evaluation) => {
    return ajax.post("/teacher/addEvaluation", data);
};

// 获取老师列表
export const getTeacherList = (params: {
    pageNum?: number;
    pageSize?: number;
}) => {
    return ajax.get("/teacher/getList", {
        params
    });
};

// 根据ID获取老师
export const getTeacherById = (id: number) => {
    return ajax.get("/teacher/get", {
        params: { id }
    });
};

// 添加老师
export const addTeacher = (data: any) => {
    return ajax.post("/teacher/add", data);
};

// 编辑老师
export const editTeacher = (data: any) => {
    return ajax.post("/teacher/edit", data);
};

// 删除老师
export const deleteTeacher = (id: number) => {
    return ajax.delete("/teacher/delete", {
        params: { id }
    });
};

// 搜索老师
export const searchTeachers = (data: any) => {
    // 提取分页参数和搜索条件
    const { pageNum, pageSize, ...searchParams } = data;
    return ajax.post("/teacher/Search", searchParams, {
        params: {
            pageNum,
            pageSize
        }
    });
};

// 加入小组
export const joinGroup = (teacherID: number, groupID: string) => {
    return ajax.post("/teacher/join", null, {
        params: { teacherID, groupID }
    });
};

// 退出小组
export const exitGroup = (teacherID: number, groupID: string) => {
    return ajax.post("/teacher/exit", null, {
        params: { teacherID, groupID }
    });
};


// 获取小组列表
export const getGroupList = () => {
    return ajax.get("/group/getList");
};

// 获取教师事件日志
export const getTeacherEvents = (teacherId: number) => {
    return ajax.get<ApiResponse<EventLog[]>>("/teacher/getLog", {
        params: { id: teacherId }
    });
};

export const resetPassword = (id: number, password: string) => {
    return ajax.post("/teacher/resetPassword", null, {
        params: { id, password }
    });
};